package com.lg.system.menu.controller;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.menu.entity.Menu;
import com.lg.system.menu.service.MenuService;
import com.lg.utils.Query;
import com.lg.utils.R;
import com.lg.utils.Tree;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-03-21
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController<Menu, MenuService> {

	private String prefix="system/menu/";
	@Autowired
	MenuService menuService;
	
	@RequiresPermissions("menu:menu")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@GetMapping("/fontawesome")
	public ModelAndView fontawesome(){
		ModelAndView mView=new ModelAndView(prefix+"fontawesome");
		return mView;
	}
	
	@RequiresPermissions("menu:menu")
	@RequestMapping("/list")
	public List<Menu> selectList(Menu menu){
		Query<Menu> query = new Query<Menu>(menu,false);
		List<Menu> menus=menuService.selectList(query.getEntityWrapper());
		return menus;
	}
	
	@RequiresPermissions("menu:add")
	@GetMapping("/add/{id}")
	public ModelAndView add(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"add");
		if(id.intValue()==0){
			mView.addObject("name", "根目录");
		}else{
			mView.addObject("name", menuService.selectById(id).getName());
		}
		mView.addObject("id", id);
		return mView;
	}
	
	@RequiresPermissions("menu:edit")
	@GetMapping("/edit/{menuId}")
	public ModelAndView edit(@PathVariable("menuId") Long menuId){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		Menu menu= menuService.selectById(menuId);
		mView.addObject("obj",menu);
		if(menu.getParentId().intValue()==0){
			mView.addObject("pname",menu.getName());
		}else{
			Menu pmenu=menuService.selectById(menu.getParentId());
			mView.addObject("pname",pmenu.getName());
		}
		return mView;
	}
	
	@RequiresPermissions("menu:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) Menu menu,BindingResult bindingResult){
		if(menuService.save(menu)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("menu:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) Menu menu,BindingResult bindingResult){
		if(menuService.update(menu)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("menu:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) Menu menu,BindingResult bindingResult){
		if(menuService.deleteById(menu.getMenuId())){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequestMapping("/selectTreeList")
	public Tree<Menu> selectTreeList(){
		Tree<Menu> menus=menuService.selectTreeList();
		return menus;
	}
	
}
